<template>
  <div class="dialog">
    <el-dialog
      :title="'供应商'+titleText"
      :visible.sync="showDialog"
      width="40%"
      top="0"
      :close-on-click-modal="false"
      @close="cancel"
    >
      <el-form
        ref="ruleForm"
        label-position="left"
        :model="formData"
        :rules="rules"
        class="el-form"
      >
        <el-form-item label="供应商名称	：" class="w50p" prop="supplierName">
          <el-input
            v-model="formData.supplierName"
            clearable
            placeholder="请输入供应商名称"
          />
        </el-form-item>
        <el-form-item label="负责人 ：" class="w50p" prop="contactPerson">
          <el-input
            v-model="formData.contactPerson"
            clearable
            placeholder="请输入负责人姓名"
          />
        </el-form-item>
        <el-form-item label="联系方式 ：" class="w50p" prop="contactPhone">
          <el-input
            v-model="formData.contactPhone"
            clearable
            placeholder="请输入负责人联系电话"
          />
        </el-form-item>
        <el-form-item label="供应商地址:" class="w100p" prop="address">
          <el-cascader
            v-model="address"
            size="large"
            :options="options"
            clearable
            @change="handleChange"
          />
          <el-input
            v-model="formData.addressDetail"
            placeholder="请输入详细地址"
            clearable
          />
        </el-form-item>
        <el-form-item label="主要物料	：" class="w50p" prop="resources">
          <el-input
            v-model="formData.resources"
            clearable
            placeholder="请输入供应商主要提供物料"
          />
        </el-form-item>
        <el-form-item label="开户行	：" class="w50p" prop="depositBank">
          <el-input
            v-model="formData.depositBank"
            clearable
            placeholder="请输入开户行"
          />
        </el-form-item>
        <el-form-item label="银行账号	：" class="w50p" prop="bankAccount">
          <el-input
            v-model="formData.bankAccount"
            clearable
            placeholder="请输入银行卡号"
          />
        </el-form-item>
        <el-form-item label="营业执照	：" class="w50p" prop="businessLicense">
          <el-input
            v-model="formData.businessLicense"
            clearable
            placeholder="请输入营业执照编号"
          />
        </el-form-item>
        <el-form-item label="统一社会信用代码	：" class="w50p" prop="uniformSocialCreditCode">
          <el-input
            v-model="formData.uniformSocialCreditCode"
            clearable
            placeholder="请输入统一社会信用代码"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import SupplierApi from "@/api/business-manage/supplier"

import { regionData, CodeToText, TextToCode } from "element-china-area-data";

export default {
  name: '',
  components: { },
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    }
  },
  data() {
    let checkInt = (rule, value, callback) => {
      if (value) {
        if (Number(value) && value % 1 === 0) {
          callback();
        } else {
          return callback(new Error("请输入整数！"));
        }
      }
    };
    return {
      //省市区地址数组
      address:'',
      formData: {},
      staffList: [],
      warehouseList: [],
      options: regionData,
      rules: {
        supplierName: [
          { required: true, message: "请输入供应商名称", trigger: ["change", "blur"] }
        ],
        contactPerson: [
          { required: true, message: "请输入负责人称呼", trigger: ["change", "blur"] }
        ],
        contactPhone: [
          { required: true, message: "请输入负责人联系方式", trigger: ["change", "blur"] }
        ],
        address: [
          { required: true, message: "请输入供应商地址", trigger: ["change", "blur"] }
        ],
      },
      showDialog: this.show
    };
  },
  computed: {
  },
  watch: {
    show(v) {
      this.showDialog = v;
    },
    nowChanceStau(v) {
      if (v === 'add') {
        this.formData = {
          staffName: sessionStorage.getItem('staffName'),
        }
      }
      if (v === 'edit') {
        this.formData = this.editObject
        if (this.editObject.address) {
          let dizhi = this.editObject.address.split(",", 3);
          this.address = [
            TextToCode[dizhi[0]].code,
            TextToCode[dizhi[0]][dizhi[1]].code,
            TextToCode[dizhi[0]][dizhi[1]][dizhi[2]].code
          ];
          // this.formData.address = this.address
        }
      }
    }
  },
  created() {
  },
  mounted() {
  },
  methods: {
    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            SupplierApi.edit(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          } else if (this.nowChanceStau === "add") {
            SupplierApi.add(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("添加成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    // 选择省市区地址
    handleChange(value) {
      // this.formData.address = value;
      let name = "";
      this.address.map(item => {
        name += CodeToText[item]+',';
      });
      this.formData.address = name;
      console.log(name);
    },
    close() {
      this.$emit("closeDialog");
    },
    cancel() {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped>
</style>
